package com.valco.app;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/com/valco/app/applicationContext.xml")
public class UserDaoTest{
	@Autowired
	private ApplicationContext context;
	
	@Autowired
	private UserDao dao;
	
	User user1;
	User user2;
	User user3;
	@Before
	public void setUp(){ 
		this.user1 = new User("zilalxx", "joosang", "1234");
		this.user2 = new User("zilalxxx", "joosang", "1234");
		this.user3 = new User("zilalxxxx", "joosang", "1234");
		
	}
	
	@Test(expected=EmptyResultDataAccessException.class)
	public void getUserFailure() throws SQLException{
		
		this.dao.deleteAll();
		
		assertThat(this.dao.getCount(), is(0));
		
		this.dao.get("unknown_id");
		
	}
	
	
	@Test
	public void addAndGet() throws SQLException{

		
		dao.deleteAll();
		assertThat(dao.getCount(), is(0));
		
		dao.add(user1);
		dao.add(user2);
		assertThat(dao.getCount(), is(2));
		 
		User userget1 = dao.get(user1.getId()); 
		assertThat(userget1.getName(), is(user1.getName()));
		assertThat(userget1.getPassword(), is(user1.getPassword()));

		User userget2 = dao.get(user2.getId()); 
		assertThat(userget2.getName(), is(user2.getName()));
		assertThat(userget2.getPassword(), is(user2.getPassword()));
		
		 
		
		
	}
	
	@Test
	public void count() throws SQLException{

		
		
		dao.deleteAll();
		assertThat(dao.getCount(), is(0));
		
		dao.add(user1);
		assertThat(dao.getCount(), is(1));
		
		dao.add(user2);
		assertThat(dao.getCount(), is(2));
		
		dao.add(user3);
		assertThat(dao.getCount(), is(3));
		  
	}
	
//	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args)throws ClassNotFoundException,SQLException {
//		// TODO Auto-generated method stubv
//	JUnitCore.main("com.valco.app.UserDaoTest");
//	}

}
